package com.aid.winter.controller;

import com.aid.winter.common.annotation.RequiredRole;
import com.aid.winter.common.constant.RoleConstants;
import com.aid.winter.common.response.Result;
import com.aid.winter.dto.request.UserCreateRequest;
import com.aid.winter.dto.request.UserUpdateRequest;
import com.aid.winter.dto.response.PageResult;
import com.aid.winter.dto.response.UserVO;
import com.aid.winter.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理控制器
 */
@RestController
@RequestMapping("/manage/user")
@RequiredArgsConstructor
public class SysUserManageController {

    private final SysUserService userService;

    /**
     * 创建用户（仅管理员可操作）
     */
    @PostMapping
    @RequiredRole(RoleConstants.ROLE_ADMIN)
    public Result<UserVO> createUser(@Validated @RequestBody UserCreateRequest request) {
        return Result.ok(userService.createUser(request));
    }

    /**
     * 更新用户信息（仅管理员可操作）
     */
    @PutMapping("/{id}")
    @RequiredRole(RoleConstants.ROLE_ADMIN)
    public Result<UserVO> updateUser(
            @PathVariable Long id,
            @Validated @RequestBody UserUpdateRequest request) {
        return Result.ok(userService.updateUser(id, request));
    }

    /**
     * 重置用户密码（仅管理员可操作）
     */
    @PostMapping("/{id}/reset-password")
    @RequiredRole(RoleConstants.ROLE_ADMIN)
    public Result<Void> resetPassword(@PathVariable Long id) {
        userService.resetPassword(id);
        return Result.ok();
    }

    /**
     * 更新用户状态（仅管理员可操作）
     */
    @PutMapping("/{id}/status")
    @RequiredRole(RoleConstants.ROLE_ADMIN)
    public Result<Void> updateStatus(
            @PathVariable Long id,
            @RequestParam Integer status) {
        userService.updateStatus(id, status);
        return Result.ok();
    }

    /**
     * 查询用户列表（仅管理员可操作）
     */
    @GetMapping("/list")
    @RequiredRole(RoleConstants.ROLE_ADMIN)
    public Result<PageResult<UserVO>> listUsers(
            @RequestParam(required = false) Integer roleType,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String realName,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.ok(userService.listUsers(roleType, username, realName, pageNum, pageSize));
    }
} 